<template>
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="120px" v-show="showSearch">
        <el-form-item label="省" prop="provinceId">
            <el-select v-model="queryParams.provinceId" placeholder="请选择省" @change="chooseProvince" clearable>
                <el-option v-for="item in provinceData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="市" prop="cityId">
            <el-select v-model="queryParams.cityId" placeholder="请选择市" @change="chooseCity" clearable>
                <el-option v-for="item in cityData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="区/县" prop="countiesId">
            <el-select v-model="queryParams.countiesId" placeholder="请选择区/县" clearable>
                <el-option v-for="item in areaData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="报告名称" prop="srName">
            <el-input v-model="queryParams.srName" placeholder="请输入报告名称" clearable />
        </el-form-item>
        <el-form-item label="报告类型" prop="srType">
            <el-select v-model="queryParams.srType" placeholder="请选择报告类型" clearable>
                <el-option v-for="item in optionType" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="handleQuery">搜索</el-button>
            <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
            <el-button type="primary" @click="handleDownloads" :disabled="multiple">
                <svg-icon class-name="download-icon" icon-class="download" /><span style="margin-left:10px">批量下载</span>
            </el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" stripe @selection-change="handleSelectionChange" row-key="id">
        <el-table-column type="selection" width="55" align="center" :reserve-selection="true" />
        <el-table-column label="报告ID" align="center" prop="srNumber" />
        <el-table-column label="报告名称" align="center" prop="srName" width="255" show-overflow-tooltip />
        <el-table-column label="提交人" align="center" prop="submiter" />
        <el-table-column label="报告类型" align="center" prop="srType" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
                <el-button type="text" @click="handleDownload(scope.row)">报告下载</el-button>
                <el-button type="text" @click="uploadIdea(scope.row)">上传评审意见</el-button>
            </template>
        </el-table-column>
    </el-table>

    <!-- <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"
        @pagination="getList" /> -->

    <!-- 评审意见对话框 -->
    <el-dialog v-model="openIdea" title="评审意见" width="500px" append-to-body>
        <p></p>
        <el-form ref="ideaRef" :model="form" :rules="rules" label-width="150px">
            <el-form-item label="请上传评审意见" prop="uploadIdeaId">
                <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                    @click="importTemplate('评审意见模板.doc')">下载模板</el-link>
                <file-upload ref="fileUploadIdea" v-model="form.uploadIdea" :limit="limit" :fileType="['doc', 'docx', 'pdf']" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="confirmUpload">确 定</el-button>
                <el-button @click="openIdea = false">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>
  
<script setup>
import { getProvince, getChildArea } from "@/api/system/division";
import { getAllList, updateExpert } from "@/api/business/expertPending.js"
const { proxy } = getCurrentInstance();
const dataList = ref([]);
const ids = ref();
const loading = ref(false);
const single = ref(true);
const multiple = ref(true);
const openIdea = ref(false);
const total = ref(0);
const showSearch = ref(true);
const id = ref();
const limit = ref(1);
const provinceData = ref([]); //省
const cityData = ref([]); //市
const areaData = ref([]); //区、县
const optionType = [{ label: '地震灾害风险评估报告', value: '地震灾害风险评估报告' }, { label: '辅助对策报告', value: '辅助对策报告' }]

const data = reactive({
    queryParams: {
        // page: 1,
        // rows: 10,
        shStatus: 0,
        provinceId: undefined,
        cityId: undefined,
        countiesId: undefined,
        srName: undefined,
        srType: undefined,
    },
    form: {
        uploadIdeaId: undefined,
        uploadIdea: [],
    },
    rules: {
        uploadIdeaId: [{ required: true, message: "上传评审意见不能为空", trigger: "blur" }],
    }
})
const { queryParams, form, rules } = toRefs(data)


/** 搜索按钮操作 */
function handleQuery() {
    // queryParams.value.page = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    dataList.value = [];
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 查询数据列表 */
function getList() {
    loading.value = true;
    getAllList(queryParams.value).then(response => {
        // dataList.value = response.data.actReports ? response.data.actReports : [];
        // total.value = response.data.total;
        dataList.value = response.data ? response.data : [];
        loading.value = false;
    });
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.attachId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 批量下载操作 */
function handleDownloads() {
    proxy.download("/attachment/downForZip/"+ ids.value.join(","), { }, '报告.zip');
};

// 单个下载文件
function handleDownload(row) {
    var query = {
        attachId: row.attachId,
        name: row.srName
    }
    location.href = import.meta.env.VITE_APP_BASE_API + "/attachment/downLoadFile?attachId=" + encodeURIComponent(query.attachId);
}

// 模板下载
function importTemplate(name) {
    proxy.download("/fxfzaqbz/data/downloadExcel", {
        fileName: name
    }, name);
};

// 上传意见
function confirmUpload() {
    const fileList = proxy.$refs["fileUploadIdea"] ? JSON.parse(
                JSON.stringify(proxy.$refs["fileUploadIdea"].fileList)
            ) : [];
            const attachIds = [];
            for (var i = 0; i < fileList.length; i++) {
                attachIds.push(fileList[i].attachId);
            }
            form.value.uploadIdeaId = attachIds.join(",");
    proxy.$refs["ideaRef"].validate(valid => {
        if (valid) {    
            updateExpert({ opinion: form.value.uploadIdeaId,id:id.value }).then(res => {
                proxy.$modal.msgSuccess("上传意见成功");
                getList();
                openIdea.value = false;
            })
        }
    });
}

// 打开评审意见
function uploadIdea(row) {
   form.value.uploadIdea = [];
    openIdea.value = true;
    proxy.resetForm("ideaRef");
    id.value = row.id;
}

/** 获取省列表 */
function getProvinceData() {
    getProvince().then((response) => {
        provinceData.value = response.data;
    });
}

function chooseProvince(value) {
    queryParams.value.cityId = null;
    queryParams.value.countiesId = null;
    cityData.value = [];
    areaData.value = [];
    getChildArea(value).then(response => {
        cityData.value = response.data;
    });
}

function chooseCity(value) {
    queryParams.value.countiesId = null;
    areaData.value = [];
    getChildArea(value).then((response) => {
        areaData.value = response.data;
    });
}


getProvinceData();
getList();
</script>
  
<style lang="scss" scoped>
.hsdTime {
    margin-right: 0px !important;

}
</style>